package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Activity;
import cn.wolfcode.domain.ActivityItem;
import cn.wolfcode.domain.Choice;
import cn.wolfcode.enums.ActivityTypeEnum;
import cn.wolfcode.exception.LogicException;

import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.service.IActivityItemService;
import cn.wolfcode.service.IActivityService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;
    @Autowired
    private IActivityItemService activityItemService;


    @RequiredPermission(name = "活动页面", expression = "activity:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        PageInfo<Activity> pageInfo = activityService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("enums", ActivityTypeEnum.values());
        return "activity/list";
    }


    @RequiredPermission(name = "活动删除", expression = "activity:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            activityService.delete(id);
        }
        return "redirect:/activity/list";
    }


    @RequiredPermission(name = "活动新增/编辑", expression = "activity:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Activity activity) {
        if (activity.getId() == null) {
            activityService.save(activity);
        } else {
            activityService.update(activity);
        }
        return "redirect:/activity/list";
    }


    @RequestMapping("/vote")
    @ResponseBody
    public JsonResult vote(Long choiceId, Long activityId) {
        //activityService.getStatusById(id);
        //查询是否已经投票
        //Choice choice = activityService.queryRelation(choiceId);

        activityService.queryRelation(activityId);


        activityService.updateActivityItem(choiceId);
        activityService.insertRelation(choiceId);
        return new JsonResult();
    }


    @RequiredPermission(name = "活动新增/编辑", expression = "activity:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            //model.addAttribute("activity", activityService.get(id));
            //activityService.getActivityItemByActivityId(id);

            model.addAttribute("activity", activityService.get(id));
            List<ActivityItem> item = activityItemService.queryByActivityId(id);
            model.addAttribute("item", item);
        }
        return "activity/input";
    }

    @RequiredPermission(name = "活动新增/编辑", expression = "activity:saveOrUpdate")
    @RequestMapping("/result")
    public String updateResult(Long activityId, Model model) {
        model.addAttribute("activity", activityService.get(activityId));
        List<ActivityItem> items = activityItemService.queryByActivityId(activityId);
        model.addAttribute("items", items);
        //activityService.insertRelation(activityId);
        return "/activity/result";
    }

    @RequestMapping("/status")
    @ResponseBody
    public JsonResult status(Long id) {
        activityService.getStatusById(id);
        return new JsonResult();
    }

}
